<template>
  <div>
    <!-- 头部导航栏 -->
    <div class="header">
      <van-nav-bar
        class="titlenav"
        title="实名认证"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
      />
    </div>
    <!-- 中间图片 -->
    <div class="tip">
      <div class="icon">
        <img src="../../../public/safety.png" alt="" width="100" height="100" />
      </div>
      <p class="tip1">进行实名认证</p>
      <p class="tip2">用于保障账号的安全及快速接收订单信息</p>
    </div>
    <!-- 表单数据上传 -->
    <div class="Formdata">
      <van-form>
        <!-- 真实姓名 -->
        <van-field
          v-model="form.realname"
          name="真实姓名"
          label="真实姓名"
          placeholder="真实姓名"
          :rules="[{ required: true, message: '请填写姓名' }]"
        />
        <!-- 身份证号 -->
        <van-field
          v-model="form.idCard"
          name="身份证号"
          label="身份证号"
          placeholder="身份证号"
          :rules="[{ required: true, message: '请填写身份证号' }]"
        />
        <!-- 银行卡号 -->
        <van-field
          v-model="form.bankCard"
          name="银行卡号"
          label="银行卡号"
          placeholder="银行卡号"
          :rules="[{ required: true, message: '请填写银行卡号' }]"
        />

        <!-- 身份证正面照 -->
        <van-field name="uploader" label="身份证正面照">
          <template #input>
            <van-uploader
              :after-read="afterRead1"
              v-model="fileList1"
              :max-count="1"
            />
          </template>
        </van-field>
        <!-- 身份证反面照 -->
        <van-field name="uploader" label="身份证反面照">
          <template #input>
            <van-uploader
              :after-read="afterRead2"
              v-model="fileList2"
              :max-count="1"
            />
          </template>
        </van-field>
        <!-- 银行卡照片 -->
        <van-field name="uploader" label="银行卡照片">
          <template #input>
            <van-uploader
              :after-read="afterRead3"
              v-model="fileList3"
              :max-count="1"
            />
          </template>
        </van-field>
        <!-- 提交按钮 -->
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit" @click="submitHandler">提交</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex";
import {post_formData,post} from '../../http/axios'
import {Toast} from 'vant'
export default {
  data() {
    return {
      // 表单数组
      form: {
        
      },
     fileList1:[],
     fileList2:[],
     fileList3:[]
    };
  },

  computed: {
      ...mapState("user", ["userInfo"]),
  },

  methods: {

    //   上传身份证正面照
    afterRead1(file, detail) {
    //   console.log(this.dataURLtoFileFun(file.content));
      let formData = new FormData();
      formData.append('file',this.dataURLtoFileFun(file.content,'file.jpg'))
      post_formData('http://121.199.29.84:8001/file/upload',formData).then(res=>{
        //  console.log(res.data);
        if(res.data.status === 200){
            this.form.idcardPhotoPositive ='http://121.199.29.84:8888/'+res.data.data.groupName+'/'+res.data.data.id
            this.fileList1 = [{url:'http://121.199.29.84:8888/'+res.data.data.groupName+'/'+res.data.data.id}]
        }
      })
    },

    // 上传身份证反面照
    afterRead2(file, detail) {
    //   console.log(this.dataURLtoFileFun(file.content));
      let formData = new FormData();
      formData.append('file',this.dataURLtoFileFun(file.content,'file.jpg'))
      post_formData('http://121.199.29.84:8001/file/upload',formData).then(res=>{
        // console.log(res.data.data.groupName);
        if(res.data.status === 200){
            this.form.idcardPhotoNegative ='http://121.199.29.84:8888/'+res.data.data.groupName+'/'+res.data.data.id
            this.fileList2 = [{url:'http://121.199.29.84:8888/'+res.data.data.groupName+'/'+res.data.data.id}]
            
        }
      })
    },

    // 上传银行卡照片
    afterRead3(file, detail) {
    //   console.log(file);
      let formData = new FormData();
      formData.append('file',this.dataURLtoFileFun(file.content,'file.jpg'))
      post_formData('http://121.199.29.84:8001/file/upload',formData).then(res=>{
        // console.log(res.data);
        if(res.data.status === 200){
            this.form.bankCardPhoto ='http://121.199.29.84:8888/'+res.data.data.groupName+'/'+res.data.data.id
            this.fileList3 = [{url:'http://121.199.29.84:8888/'+res.data.data.groupName+'/'+res.data.data.id}]
        }
      })
    },

    // bae64转文件对象
    dataURLtoFileFun (dataurl, filename) {
      // 将base64转换为文件，dataurl为base64字符串，filename为文件名（必须带后缀名，如.jpg,.png）
      let arr = dataurl.split(",");
      let mime = arr[0].match(/:(.*?);/)[1];
      let bstr = atob(arr[1]);
      let n = bstr.length;
      let u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new File([u8arr], filename, { type: mime });
    },

    //   表单提交方法
    submitHandler(){
        this.form.userId =this.userInfo.id
    //   console.log(this.form);
      post('/certification/submitCertificationApply',this.form).then(res=>{
        console.log(res.data.status);
        if(res.data.status === 200){
          Toast(res.data.message)
          this.$router.push('/manager/mine')
        }else {
          Toast(res.data.message)
        }
      })
    },


    //点击返回到上一个页面
    onClickLeft() {
      this.$router.go(-1);
    },
  },

  created() {},
};
</script>

<style scoped>
.header {
  height: 58px;
  /* background-image: linear-gradient(90deg, #bf73ff, #7579ff); */
  text-align: center;
  line-height: 58px;
  color: #fff;
  letter-spacing: 0.2em;
}
.titlenav {
  height: 58px;
  text-align: center;
  line-height: 58px;
}
.tip .icon {
  width: 100px;
  height: 100px;
  margin: 0 auto;
  margin-top: 0px;
  margin-top: 1em;
  border-radius: 50%;
  background-color: #ddfae7;
}
.tip .tip1 {
  margin: 0 auto;
  margin-top: 0px;
  text-align: center;
  margin-top: 1em;
  font-weight: 300;
}
.tip .tip2 {
  margin: 0 auto;
  margin-top: 0px;
  text-align: center;
  margin-top: 1em;
  font-weight: 300;
  width: 80%;
  color: #999;
  font-size: 12px;
  letter-spacing: 0.1em;
}
.Formdata {
  margin-top: 2em;
  padding: 0 0.5em;
}
</style>